package com.quickcancellation.crm.config;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @Author: 麦翔盛
 * @Description: 这个是日志切面配置类 日志在log目录下
 * @Date: Created in 14:33 2022/8/8
 * @Modified By:
 */
@Component
@Aspect
public class LogAopConfig {

    private final static Logger logger = LoggerFactory.getLogger(LogAopConfig.class);

    /**
     * 注解的位置
     */
    @Pointcut("@annotation(com.quickcancellation.crm.config.LogAspect)")
    public void logPointCut() {}


   // @Around("execution(* com.example.demo.service.impl..*.*(..))")
//    @Around("logPointCut()")
//    public Object process(ProceedingJoinPoint point) throws Throwable {
//        System.out.println("@Around：执行目标方法之前...");
//        //访问目标方法的参数：
//        Object[] args = point.getArgs();
//        if (args != null && args.length > 0 && args[0].getClass() == String.class) {
//         //   args[0] = "改变后的参数1";
//        }
//        //用改变后的参数执行目标方法
//        Object returnValue = point.proceed(args);
//        System.out.println("@Around：执行目标方法之后...");
//        System.out.println("@Around：被织入的目标对象为：" + point.getTarget());
//        return "原返回值：" + returnValue + "，这是返回结果的后缀";
//    }

  //  @Before("execution(* com.example.demo.service.impl..*.*(..))")
    @Before("logPointCut()")
    public void permissionCheck(JoinPoint point) {
  //      System.out.println("@Before：模拟权限检查...");
        logger.info("\n-------------------正在执行:" +
                point.getSignature().getDeclaringTypeName() +
                "." + point.getSignature().getName()+"()-------------------\n参数为[ " + JSONObject.toJSONString(point.getArgs()) + " ]");
    //    logger.info("@Before：参数为：" + Arrays.toString(point.getArgs()));
     //   System.out.println("@Before：被织入的目标对象为：" + point.getTarget());
    }

 //   @AfterReturning(pointcut="execution(* com.example.demo.service.impl..*.*(..))",
  //          returning="returnValue")
    @AfterReturning(pointcut = "logPointCut()",returning="returnValue")
    public void log(JoinPoint point, Object returnValue) {
     //   System.out.println("@AfterReturning：模拟日志记录功能...");
//        logger.info("@AfterReturning：目标方法为：" +
//                point.getSignature().getDeclaringTypeName() +
//                "." + point.getSignature().getName());
//        System.out.println("@AfterReturning：参数为：" +
//              //  Arrays.asList(point.getClass()+"::"+point.getArgs()));
//                Arrays.toString(point.getArgs()));
        logger.info("\n返回值为[ " + JSONObject.toJSONString(returnValue) + " ]"+"\n-------------------执行完成:"+point.getSignature().getDeclaringTypeName() +
                "." + point.getSignature().getName()+"()-------------------");
      //  System.out.println("@AfterReturning：被织入的目标对象为：" + point.getTarget());
    }

    //@After("execution(* com.example.demo.service.impl..*.*(..))")
//    @After("logPointCut()")
//    public void releaseResource(JoinPoint point) {
//        System.out.println("@After：模拟释放资源...");
//        System.out.println("@After：目标方法为：" +
//                point.getSignature().getDeclaringTypeName() +
//                "." + point.getSignature().getName());
//        System.out.println("@After：参数为：" + Arrays.toString(point.getArgs()));
//        System.out.println("@After：被织入的目标对象为：" + point.getTarget());
//    }


    //后置异常通知
    @AfterThrowing(throwing = "ex",pointcut = "logPointCut()")
    public void throwss(JoinPoint joinPoint,Throwable ex){
        String declaringTypeName = joinPoint.getSignature().getDeclaringTypeName();
        String name = joinPoint.getSignature().getName();
        logger.error(declaringTypeName+"."+name+"异常:   方法异常时执行....."+ex.toString());
    }
}
